<template>
  <div class="app-container">
    <el-container v-if="authStore.isAuthenticated">
      <el-aside class="sidebar">
        <div class="logo-container">
          <img src="@/assets/logo.png" alt="Logo" class="logo">
        </div>
        <el-menu
          :default-active="route.path"
          class="el-menu-vertical"
          router
          background-color="#182132"
          text-color="#96a2b9"
          active-text-color="#fff">
          <el-menu-item index="/">
            <el-icon><HomeFilled /></el-icon>
            <span>首页</span>
          </el-menu-item>
          <el-menu-item index="/clusters">
            <el-icon><Connection /></el-icon>
            <span>集群管理</span>
          </el-menu-item>
          <el-menu-item index="/nodes">
            <el-icon><Monitor /></el-icon>
            <span>节点管理</span>
          </el-menu-item>
          <el-menu-item index="/instances">
            <el-icon><Cpu /></el-icon>
            <span>实例管理</span>
          </el-menu-item>
          <el-menu-item index="/dashboard">
            <el-icon><DataLine /></el-icon>
            <span>系统仪表盘</span>
          </el-menu-item>
          <el-menu-item index="/analytics">
            <el-icon><TrendCharts /></el-icon>
            <span>行为分析</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      
      <el-container>
        <el-header class="header">
          <div class="header-left">
            <h2 class="logo">云资源管理系统</h2>
          </div>
          <div class="header-right">
            <el-dropdown @command="handleCommand">
              <span class="user-dropdown">
                {{ authStore.userFullName }}
                <el-icon><ArrowDown /></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="logout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </el-header>
        
        <el-main class="main-content">
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
    
    <router-view v-else></router-view>
  </div>
</template>

<script setup>
import { useRoute } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { ElMessage } from 'element-plus'
import {
  HomeFilled,
  Connection,
  Monitor,
  Cpu,
  DataLine,
  ArrowDown,
  TrendCharts
} from '@element-plus/icons-vue'

const route = useRoute()
const authStore = useAuthStore()

// 初始化认证状态
authStore.initializeAuth()

const handleCommand = async (command) => {
  if (command === 'logout') {
    try {
      await authStore.logout()
      ElMessage.success('退出登录成功')
    } catch (error) {
      ElMessage.error('退出登录失败')
    }
  }
}
</script>

<style scoped>
.logo-container {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #0f1a2d;
}

.logo {
  height: 32px;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.header-left {
  display: flex;
  align-items: center;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-dropdown {
  display: flex;
  align-items: center;
  cursor: pointer;
  color: #63656e;
}

.user-dropdown .el-icon {
  margin-left: 4px;
  font-size: 12px;
}

.el-menu-vertical {
  border-right: none;
}

.el-menu-vertical:not(.el-menu--collapse) {
  width: 240px;
}
</style>
